﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.DataAccess;

namespace University.Workshop.WinApp
{
    public partial class AdvancedSearch : Form
    {
        public AdvancedSearch()
        {
            InitializeComponent();
        }

        private void buttonBuscar_Click(object sender, EventArgs e)
        {
            string fullname = string.Empty;
            string id = string.Empty;
            string telephone = string.Empty;

            fullname = textBoxNombre.Text;
            id = textBoxCarnet.Text;
            telephone = textBoxTelefono.Text;

            DataTable dtb = new DataTable();
            dtb = ClientSearcher.Search(fullname, id, telephone);


            try
            {
                try
                {
                    this.GridViewBuscar.DataSource = dtb;

                    labelInfo.Text = String.Format("Cantidad de registros encontrados: {0}", dtb.Rows.Count);
                }
                catch (Exception ex)
                {
                    labelInfo.Text = "Error: " + ex.Message;
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("ERROR: " + ex.Message, " Comprobar tabla");
            }
        }

        private void buttonLimpiar_Click(object sender, EventArgs e)
        {
            textBoxNombre.Text = null;
            textBoxCarnet.Text = null;
            textBoxTelefono.Text = null;
        }

        private void buttonCancelar_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonHistorial_Click(object sender, EventArgs e)
        {
            int fila;
            fila = int.Parse(GridViewBuscar[0, GridViewBuscar.CurrentCell.RowIndex].Value.ToString());

            DataTable dtb = new DataTable();
           // dtb = ClientSearcher.SearchHistory(fila);

            try
            {
                try
                {
                    this.GridViewHistorial.DataSource = dtb;
                }
                catch (Exception ex)
                {
                    labelInfo.Text = "Error: " + ex.Message;
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("ERROR: " + ex.Message, " Comprobar tabla");
            }

        }

        private void buttonReservas_Click(object sender, EventArgs e)
        {
            textBoxNombre.Text = GridViewBuscar[1, GridViewBuscar.CurrentCell.RowIndex].Value.ToString();
            textBoxCarnet.Text = GridViewBuscar[2, GridViewBuscar.CurrentCell.RowIndex].Value.ToString();
            textBoxTelefono.Text = GridViewBuscar[3, GridViewBuscar.CurrentCell.RowIndex].Value.ToString();

            string fullname = string.Empty;
            string id = string.Empty;
            string telephone = string.Empty;

            fullname = textBoxNombre.Text;
            id = textBoxCarnet.Text;
            telephone = textBoxTelefono.Text;

            int fila;
            fila = int.Parse(GridViewBuscar[0, GridViewBuscar.CurrentCell.RowIndex].Value.ToString());

            ClientUpdater.UpdateClient(fullname, id, telephone, fila);


        }

    }
}
